<?php
class ControllerCommonLogin extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('common/login');

		$this->document->setTitle("login");

		if (($this->request->server['REQUEST_METHOD'] == 'GET') && isset($this->request->get['userid'])) {
			$this->load->model('user/user');
			$this->model_user_user->activeUser($this->request->get['userid']);
			$data['active'] = $this->request->get['userid'];
		}

		if ($this->user->isLogged() && isset($this->request->get['token']) && ($this->request->get['token'] == $this->session->data['token'])) {
			$this->response->redirect($this->url->link('memory/viewreminder', 'token=' . $this->session->data['token'], true));
		}

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->session->data['token'] = token(32);
			$this->session->data['username'] = $this->request->post['username'];
			$this->load->model('user/user');
			$result = $this->model_user_user->getUseridByUsername($this->request->post['username']);
			$this->session->data['user_id'] = $result['user_id'];
			//回到登陆前的页面
			if (isset($this->request->post['redirect']) && (strpos($this->request->post['redirect'], HTTP_SERVER) === 0 || strpos($this->request->post['redirect'], HTTPS_SERVER) === 0)) {
				$this->response->redirect($this->request->post['redirect'] . '&token=' . $this->session->data['token']);
			} else {
				$this->response->redirect($this->url->link('memory/viewreminder', 'token=' . $this->session->data['token'], true));
			}
		}
		//如果session里面有token但是请求中没有token,或者两个都有但是两个不相等，返回错误
		if ((isset($this->session->data['token']) && !isset($this->request->get['token'])) || ((isset($this->request->get['token']) && (isset($this->session->data['token']) && ($this->request->get['token'] != $this->session->data['token']))))) {
			$this->error['token'] = "登陆超时请重新登陆";
		}

		if (isset($this->error['token'])) {
			$data['error_token'] = $this->error['token'];
		} else {
			$data['error_token'] = '';
		}

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$data['success'] = '';
		}

		$data['action'] = $this->url->link('common/login', '', true);

		if (isset($this->request->post['username'])) {
			$data['username'] = $this->request->post['username'];
		} else {
			$data['username'] = '';
		}

		if (isset($this->request->post['password'])) {
			$data['password'] = $this->request->post['password'];
		} else {
			$data['password'] = '';
		}

		if (isset($this->request->get['route'])) {
			$route = $this->request->get['route'];

			unset($this->request->get['route']);
			unset($this->request->get['token']);

			$url = '';

			if ($this->request->get) {
				$url .= http_build_query($this->request->get);
			}

			$data['redirect'] = $this->url->link($route, $url, true);
		} else {
			$data['redirect'] = '';
		}

		$data['forgotten'] = $this->url->link('common/forgotten', '', true);
		$data['register']  = $this->url->link('common/register', '', ture);
		//echo "common->login->endtext<br>";//调试
		$data['header'] = $this->load->controller('common/headerout');
		$data['footerout'] = $this->load->controller('common/footerout');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('common/login', $data));
	}

	protected function validate() {
		// if (!isset($this->request->post['username']) || !isset($this->request->post['password']) || !$this->user->login($this->request->post['username'], html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8'))) {
		// 	$this->error['warning'] = "用户名密码为空，或错误！";
		// }
		if (!isset($this->request->post['username'])) {
			$this->error['warning'] = "用户名为空！";
			return !$this->error;
		}
		 if (!isset($this->request->post['password'])){
			$this->error['warning'] = "密码为空！";
			return !$this->error;
		}
		 if (!$this->user->login($this->request->post['username'], html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8'))) {
			$this->error['warning'] = "用户名不存在或密码错误！";
			return !$this->error;
		}
		  if ($result=$this->user->login($this->request->post['username'], html_entity_decode($this->request->post['password'], ENT_QUOTES, 'UTF-8')) === '2') {
		 	echo $result;
		 	$this->error['warning'] = "用户未激活，请重发电子邮件激活！";
		 	return !$this->error;
		 }

		//没有报错时是空数组，空数据是假，所以要返回真就要！;
		return !$this->error;
	}
}
